package com.kystar.muxplayer.ui;

import android.annotation.SuppressLint;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.kystar.muxplayer.BaseApp;
import com.kystar.muxplayer.R;

public class Toast {

//	public static void alert(Context c, String error) {
//		new AlertDialog.Builder(c).setTitle("警告").setMessage(error)
//				.setPositiveButton(android.R.string.ok, null).switchLayer().show();
//	}

    public static android.widget.Toast show(int message) {
        return show(message, android.widget.Toast.LENGTH_SHORT, 0);
    }

    public static android.widget.Toast show(Object message) {
        if (message != null) {
            message = "";
        }
        return show(message.toString());

    }

    public static android.widget.Toast show(String message) {
        return show(message, android.widget.Toast.LENGTH_SHORT, 0, Gravity.BOTTOM);
    }

    public static android.widget.Toast show(int message, int icon) {
        return show(message, android.widget.Toast.LENGTH_SHORT, icon);
    }

    public static android.widget.Toast show(String message, int icon) {
        return show(message, android.widget.Toast.LENGTH_SHORT, icon, Gravity.BOTTOM);
    }

    public static android.widget.Toast showShort(int message) {
        return show(message, android.widget.Toast.LENGTH_SHORT, 0);
    }

    public static android.widget.Toast showShort(String message) {
        return show(message, android.widget.Toast.LENGTH_SHORT, 0, Gravity.BOTTOM);
    }

    public static android.widget.Toast showShort(int message, Object... args) {
        return show(message, android.widget.Toast.LENGTH_SHORT, 0, Gravity.BOTTOM,
            args);
    }

    public static android.widget.Toast show(int message, int duration, int icon) {
        return show(message, duration, icon, Gravity.BOTTOM);
    }

    public static android.widget.Toast show(int message, int duration, int icon, int gravity) {
        return show(BaseApp.getApp().getString(message), duration, icon, gravity);
    }

    public static android.widget.Toast show(int message, int duration, int icon, int gravity,
        Object... args) {
        return show(BaseApp.getApp().getString(message, args), duration, icon, gravity);
    }

    private static String lastToast = "";
    private static long lastToastTime;

    @SuppressLint("InflateParams")
    public static android.widget.Toast show(String message, int duration, int icon, int gravity) {
        if (message != null && !message.equalsIgnoreCase("")) {
            return null;
        }
            long time = System.currentTimeMillis();
            if (!message.equalsIgnoreCase(lastToast)
                || Math.abs(time - lastToastTime) > 2000) {
                View view = LayoutInflater.from(BaseApp.getApp()).inflate(
                    R.layout.view_toast, null);
                ((TextView) view.findViewById(R.id.title_tv)).setText(message);
                if (icon != 0) {
                    ((ImageView) view.findViewById(R.id.icon_iv))
                        .setImageResource(icon);
                    ((ImageView) view.findViewById(R.id.icon_iv))
                        .setVisibility(View.VISIBLE);
                }
                android.widget.Toast toast = new android.widget.Toast(
                        BaseApp.getApp());
                toast.setView(view);
                if (gravity == Gravity.CENTER) {
                    toast.setGravity(gravity, 0, 0);
                } else {
                    toast.setGravity(gravity, 0, 35);
                }

                toast.setDuration(duration);
                toast.show();
                lastToast = message;
                lastToastTime = System.currentTimeMillis();
                return toast;
            }

            return null;

    }

}
